;; code for generating the plan library file

(defun lplib ()
  ;; loads this
  (load "planlib"))

(defun get-top-goal-schemas ()
  ;; returns a list of the top goals of the domain
  ;; they are found in the variable *top-goals*
  (mapcar #'(lambda (val)
	      (car val))
	  *top-goals*))

(defun get-goal-schemas ()
  ;; returns a list of the goals of the domain
  ;; in case of flat recognition, these are also the top goals
  ;; for hierarchical, they include the top and subgoals
  (if *flat-output*
      (get-top-goal-schemas)
    (get-method-schemas)))

(defun get-method-schemas ()
  ;; returns a list of *unique* method schemas from the domain
  ;; so things like get-to will just be 1 schema, even though there
  ;; are many different method bodies

  (let ((mhash (make-hash-table)))
    ;; make unique list to return
    (mapcar #'(lambda (method)
		(let ((name (schema-name method)))
		  (setf (gethash name mhash) method)))
	    ;; get a list of all methods (including duplicates)
	    (mymaphash #'(lambda (key val)
			   (method-schema val))
		       *methods*))
    (gethashvals mhash)))

(defun method-schema (method)
  ;; returns the method schema from an entry in the *methods* hash table
  (cadar method))

(defun get-operator-schemas ()
  ;; returns a list of operator schemas from domain
  (mymaphash #'(lambda (key val)
	       (operator-schema val))
	   *operators*))

(defun schema-name (schema)
  ;; returns name of a schema header
  (car schema))

(defun schema-arity (schema)
  ;; returns arity of a schema header
  (length (cdr schema)))

(defun operator-schema (operator)
  ;; given an operator, returns its schema header (in form (!op ?arg ?arg))
  (cadar operator))

(defun output-max-depth (outfile max-depth)
  ;; outputs a line saying how deep the max depth for this
  ;; library is
  (format outfile "Max Depth: ~d~%~%" max-depth))

(defun output-top-goals (outfile)
  ;; outputs a list of the top-level goals of the domain, as defined
  ;; in the <domain>_plib.lisp file as *top-goals*
  (format outfile "Top-level Goals:~%")
  (mapcar (lambda (val)
	    (format outfile
		    "~A~%"
		    (act-to-string val)))
	  (get-top-goal-schemas)))

(defun output-subgoals (outfile)
  ;; outputs all subgoals.  this includes top-goals as well, as they
  ;; could potentially be subgoals
  (format outfile "~%Subgoals:~%")
  (mapcar (lambda (val)
	    (format outfile
		    "~A~%"
		    (act-to-string val)))
	  (get-goal-schemas)))

(defun output-atomic-actions (outfile)
  (format outfile "~%Atomic Actions:~%")
  (mapcar (lambda (val)
	    (if (not (eq (car val) '!!inop)) ;; don't include internal !!INOP
		(format outfile
			"~A~%"
			(act-to-string val))))
	  (get-operator-schemas)))

(defun output-planlib (plib-filename max-depth)
  ;; main function: writes out a planlib file
  ;; which can be compiled into a plan library for perl
;; Commented out by RAIN:
;  (with-open-file (outfile plib-filename :direction :output
;			   :if-exists :supersede
;			   :if-does-not-exist :create)
;		  (output-max-depth outfile max-depth)
;		  (output-top-goals outfile)
;		  (output-subgoals outfile)
;		  (output-atomic-actions outfile)))
  )
